package com.blog.utils;

import org.springframework.stereotype.Component;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

public class IKAnalyzerUtils {
    public static List<String> split(String text, boolean useSmart) {
        List<String> result = new ArrayList<>();
        if (text == null || text.trim().isEmpty()) {
            return result;
        }

        try (StringReader reader = new StringReader(text)) {
            IKSegmenter segmenter = new IKSegmenter(reader, useSmart);
            Lexeme lexeme;
            while ((lexeme = segmenter.next()) != null) {
                result.add(lexeme.getLexemeText());
            }
        } catch (Exception e) {
            throw new RuntimeException("IK分词失败", e);
        }

        return result;
    }
}
